<?php
	include_once('objectPage.php');
	class ctl_packet extends objectPage
	{
		var $name='数据包';
		var $workground ='site';
		var $object = 'content/packet';
		var $actionView = 'content/packet/finder_action.html'; //默认的动作html模板,可以为null
		//var $filterView = 'content/packet/finder_filter.html'; //默认的过滤器html,可以为null
		var $allowImport = false;
		var $allowExport = true;

		function addNew()
		{
			$this->path[] = array('text'=>'添加数据包');
			$this->page('content/packet/packet.html');
		}

		function detail($packet_id){
			$this->path[] = array('text'=>'数据包编辑');
			$packet = $this->system->loadModel("content/packet");
			$packet = $packet->getFieldById($packet_id,array('*'));
			$baseUrl = $this->system->base_url();
			$packet['packet_href']  = $baseUrl.$packet['packet_href'];
			//$packet['link'] = 'index.php?ctl=content/packet&act=packetDownload&p[0]='.$packet['packet_id'];
			$packet['uptime'] = intval($packet['uptime']);
			$this->pagedata['packet'] = $packet;
			$this->setView('content/packet/packet.html');
			$this->output();
		}

		function save(){
			if ($filePath = $this->upload_file($_FILES))
			{
				$_POST['packet_href'] = $filePath;
			}
			$_POST['uptime'] = strtotime($_POST['uptime']);
			if ($_POST['ifpub']) $_POST['ifpub'] = 1;
			else $_POST['ifpub'] = 0;
			if($_POST['packet_id']){
				$this->begin("index.php?ctl=content/packet&act=detail&p[0]=".$_POST['packet_id']);
			}else{
				$this->begin("index.php?ctl=content/packet&act=index");
			}
			$packet = $this->system->loadModel("content/packet");
			$this->end($packet->save($_POST,$msg),$msg);
		}


		function upload_file($aData)
		{
			$uploadPath = 'home/upload/packet/';
			if (!is_dir(UPLOAD_FILE_PATH))
			{
				mkdir_p(BASE_DIR.$uploadPath,0777);
			}
			if ($aData['file']['error'] > 0)
			{
				trigger_error('上传出现错误: '.$aData['file']['error'],E_USER_ERROR);
			}
			else
			{
				$fileExt = substr($aData['file']['name'],strrpos($aData['file']['name'],'.'));	//取文件的扩展名
				$newFile = $uploadPath.date('YmdHis').$fileExt;	//得到新的文件名(根据当前的时间)
				if (is_uploaded_file($aData['file']['tmp_name']))
				{
					if (is_file(BASE_DIR.$newFile))
					{
						trigger_error($aData['file']['name'].'该文件已经存在!',E_USER_ERROR);
						return false;
					}
					if(move_uploaded_file($aData['file']['tmp_name'],BASE_DIR.$newFile)) return $newFile;
					if(copy($aData['file']['tmp_name'],BASE_DIR.$newFile))	return $newFile;
				}
			}
		}

		function packetDownload($packet_id)
		{
			header("Content-type:application/octet-stream;charset=utf-8");
			header("Content-Type: application/force-download");
			$oPacket = $this->system->loadModel('content/packet');
			$row = $oPacket->getFieldById($packet_id,array('packet_href','packet_name'));
			$fileExt = substr($row['packet_href'],strrpos($row['packet_href'],'.'));
			$oCharset = $this->system->loadModel('utility/charset');
			$fileName = $oCharset->utf2local($row['packet_name'].$fileExt,'zh');
			header("Content-Disposition:attachment;filename=".$fileName);
			//$content = $oCharset->utf2local(file_get_contents(BASE_DIR.$row['packet_href']),'zh');
			$content = file_get_contents(BASE_DIR.$row['packet_href']);
			echo $content;
		}
	}
?>